package com.wallace.downloadserver

import com.wallace.downloadserver.netty.NettyConfig
import com.wallace.downloadserver.netty.NettyServer
import com.wallace.downloadserver.service.ApplicationService
import org.mybatis.spring.annotation.MapperScan
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.BeansException
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.boot.web.servlet.ServletComponentScan
import org.springframework.scheduling.annotation.EnableAsync
import java.util.*

@SpringBootApplication
@ServletComponentScan(basePackages = ["com.wallace.downloadserver.filter"])
@MapperScan(basePackages = ["com.wallace.downloadserver.mapper"])
@EnableAsync
class DownloadserverApplication

private val log: Logger = LoggerFactory.getLogger(DownloadserverApplication::class.java)

fun main(args: Array<String>) {
    // 接口扫描
    val application = runApplication<DownloadserverApplication>(*args)
    // 设置时区
    try {
        val applicationService = application.getBean(ApplicationService::class.java)
        applicationService.scanInterface()
    } catch (e: BeansException) {
        log.error("fail to scan interfaces cause by $e")
    }
    // Netty启动
    try {
        val nettyConfig = application.getBean(NettyConfig::class.java)
        val nettyServer = application.getBean(NettyServer::class.java)
        nettyServer.start(nettyConfig.port)
    } catch (e: BeansException) {
        log.error("fail to start netty server by $e")
    }
}